import useStatus from "./useStatus";
import useUpdateStatus from "./useUpdateStatus";
import useRemove from "./useRemove";

import { statusOptions } from "@/const";
import { Space } from "antd";

export default ({
  renderEdit,
  renderModalForm,
  updateStatus,
  remove,
  actionRef,
  renderAction,
  insertPosition = 2,
  renderOtherAction,
  actionColumnWidth,
  scroll,
  onlyAction = false,
  hiddenAction = false,
  showEditBtnFunc = () => true,
}) => {
  const myRenderAction = (_, record) => {
    const showEditBtn = showEditBtnFunc(_, record, actionRef);
    const arr = [
      renderEdit
        ? renderEdit(record, actionRef)
        : showEditBtn && renderModalForm(2, record),
      updateStatus ? useUpdateStatus(updateStatus, record, actionRef) : null,
      remove ? useRemove(remove, record, actionRef) : null,
    ];
    if (renderOtherAction) {
      const result = renderOtherAction(_, record, actionRef);
      if (Array.isArray(result)) {
        arr.splice(insertPosition, 0, ...result);
      } else {
        arr.splice(insertPosition, 0, result);
      }
    }
    if (renderAction) {
      return renderAction(_, record, renderModalForm, actionRef);
    } else {
      return <Space>{arr.map((item) => item)}</Space>;
    }
  };

  const columns = [
    // {
    //   title: "登记人",
    //   dataIndex: "operatorName",
    // },
    // {
    //   title: "登记时间",
    //   dataIndex: "modidate",
    //   valueType: "dateTimeRange",
    //   render: (_, record) => {
    //     return record.modidate;
    //   },
    // },
    // {
    //   title: "状态",
    //   dataIndex: "status",
    //   valueType: "select",
    //   fieldProps: {
    //     options: statusOptions,
    //   },
    //   render: useStatus,
    // },
    {
      title: "操作",
      fixed: scroll ? "right" : false,
      dataIndex: "option",
      valueType: "option",
      width: actionColumnWidth || 200,
      render: myRenderAction,
    },
  ];

  // 没有启用/禁用方法，则不显示启用/禁用和状态列
  if (!updateStatus) {
    columns.splice(2, 1);
  }

  // 隐藏操作列
  if (hiddenAction) {
    columns.splice(-1, 1);
  }
  return onlyAction ? columns.slice(-1) : columns;
};
